﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.STAT.Utility;
using Sector7.Restaurant.Modules.STAT.LocalService;
using Sector7.Restaurant.Modules.STAT.Biz;

namespace Sector7.Restaurant.Modules.STAT.Windows
{
    /// <summary>
    /// ChargeSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ChargeSummaryWindow : UserControl
    {
        private List<RS_Charge> _charges = null;

        public ChargeSummaryWindow()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            string begin = txbBegin.Text;
            string end = txbEnd.Text;
            int beginYear, beginMonth, beginDay, endYear, endMonth, endDay;
            GenerateDataType generateType = GenerateDataType.ByDay;

            if (rdbDay.IsChecked == true)
            {
                beginYear = Convert.ToInt32(begin.Substring(0, 4));
                beginMonth = Convert.ToInt32(begin.Substring(4, 2));
                beginDay = Convert.ToInt32(begin.Substring(6, 2));

                endYear = Convert.ToInt32(end.Substring(0, 4));
                endMonth = Convert.ToInt32(end.Substring(4, 2));
                endDay = Convert.ToInt32(end.Substring(6, 2));

                generateType = GenerateDataType.ByDay;
            }
            else if (rdbMonth.IsChecked == true)
            {
                beginYear = Convert.ToInt32(begin.Substring(0, 4));
                beginMonth = Convert.ToInt32(begin.Substring(4, 2));
                beginDay = 1;

                endYear = Convert.ToInt32(end.Substring(0, 4));
                endMonth = Convert.ToInt32(end.Substring(4, 2));
                endDay = 1;

                generateType = GenerateDataType.ByMonth;
            }
            else
            {
                beginYear = Convert.ToInt32(begin.Substring(0, 4));
                beginMonth = 1;
                beginDay = 1;

                endYear = Convert.ToInt32(end.Substring(0, 4));
                endMonth = 1;
                endDay = 1;

                generateType = GenerateDataType.ByYear;
            }

            DateTime beginDate = new DateTime(beginYear, beginMonth, beginDay);
            DateTime endDate = new DateTime(endYear, endMonth, endDay).AddDays(1);

            _charges = Aide.LocalClient.Invoke<List<RS_Charge>>(
                service => service.GetAllChargesForRange(beginDate, endDate));

            graphControl.LoadData(_charges, beginDate, endDate, generateType);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DateTime now = DateTime.Now;
            int currentDayOfWeek = (int)now.DayOfWeek;

            DateTime beginDate = now.AddDays(-currentDayOfWeek);
            DateTime endDate = now.AddDays(7 - currentDayOfWeek + 1);

            // Debug
            beginDate = new DateTime(2012, 11, 24);
            endDate = new DateTime(2012, 11, 29);

            rdbDay.IsChecked = true;
            txbBegin.Text = beginDate.ToString("yyyyMMdd");
            txbEnd.Text = endDate.ToString("yyyyMMdd");

            LoadData();
        }

        private void btnStatistics_Click(object sender, RoutedEventArgs e)
        {
            LoadData();
        }
    }
}
